﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dynamicweb;

namespace DwProjectTemplate.CustomModules.ExampleModule
{
	public partial class ExampleModule_cpl : System.Web.UI.Page
	{
		protected void Page_Load( object sender, EventArgs e )
		{
			if ( Base.GetGs( "/Globalsettings/ExampleModule/ExtranetSecurity/Group" ).Length > 0 )
			{
				string groupElement = Base.GetGs( "/Globalsettings/ExampleModule/ExtranetSecurity/Group" );
				if ( groupElement.IndexOf( "," ) >= 0 )
				{
					string[] groups = groupElement.Split( ',' );
					foreach ( string group in groups )
					{
						Dynamicweb.Backend.UserGroup ug = new Dynamicweb.Backend.UserGroup( int.Parse( group.ToUpper().Replace( "GRP_", "" ) ) );
						Group.Items.Add( new ListItem( ug.GroupName, "GRP_" + ug.GroupID.ToString() ) );
					}
				}
				else
				{
					Dynamicweb.Backend.UserGroup ug = new Dynamicweb.Backend.UserGroup( int.Parse( Base.GetGs( "/Globalsettings/ExampleModule/ExtranetSecurity/Group" ).ToUpper().Replace( "GRP_", "" ) ) );
					Group.Items.Add( new ListItem( ug.GroupName, "GRP_" + ug.GroupID.ToString() ) );
				}
			}
			if ( Base.GetGs( "/Globalsettings/ExampleModule/ExtranetSecurity/SecurityGroup" ).Length > 0 )
			{
				Dynamicweb.Backend.UserGroup ug = new Dynamicweb.Backend.UserGroup( int.Parse( Base.GetGs( "/Globalsettings/ExampleModule/ExtranetSecurity/SecurityGroup" ).ToUpper().Replace( "GRP_", "" ) ) );
				SecurityGroup.Items.Add( new ListItem( ug.GroupName, "GRP_" + ug.GroupID.ToString() ) );
			}
		}
		protected void btnSave_Click( object sender, EventArgs args )
		{
			Base.SetGs( "/Globalsettings/ExampleModule/ExtranetSecurity/Group", Request.Form[ "Group" ] );
			Base.SetGs( "/Globalsettings/ExampleModule/ExtranetSecurity/SecurityGroup", Request.Form[ "SecurityGroup" ] );

			Response.Redirect( "../../Admin/Module/ControlPanel.aspx", true );
		}
		protected void btnLoadEcomGroups_Click( object sender, EventArgs args )
		{
			LoadEcomGroups();
		}

		private void LoadEcomGroups()
		{
			Dynamicweb.eCommerce.Shops.Shop s = new Dynamicweb.eCommerce.Shops.Shop( "SHOP1" );
			Dynamicweb.eCommerce.Products.GroupCollection groups = s.Groups;
			lstEcomGroups.DataSource = groups;
			lstEcomGroups.DataTextField = "Name";
			lstEcomGroups.DataValueField = "ID";
			lstEcomGroups.DataBind();
			lstEcomGroups.Visible = true;
			if ( Base.GetGs( "/Globalsettings/ExampleModule/EcomGroups" ).Length > 0 )
				lstEcomGroups.Items.FindByText( Base.GetGs( "/Globalsettings/ExampleModule/EcomGroups" ) ).Selected = true;

		}
	}
}